<article id="wikiArticle">
<p></p><p></p>
<p> <strong><code>match()</code> </strong>方法检索返回一个字符串匹配正则表达式的的结果。</p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre><code class="language-javascript"><code><em>str</em>.match(regexp)</code></code></pre>
<h3 id="Parameters" name="Parameters">参数</h3>
<dl>
<dt><code>regexp</code></dt>
<dd>一个<a href="Reference/Global_Objects/RegExp" title="JavaScript/Reference/Global Objects/RegExp">正则表达式</a>对象。如果传入一个非正则表达式对象，则会隐式地使用 <code>new RegExp(obj)</code> 将其转换为一个 <a href="Reference/RegExp" title="此页面仍未被本地化, 期待您的翻译!"><code>RegExp</code></a> 。如果你没有给出任何参数并直接使用match() 方法 ，你将会得到一 个包含空字符串的 <a href="Reference/Array" title="REDIRECT Array"><code>Array</code></a> ：[""] 。</dd>
</dl>
<h3 id="返回值">返回值</h3>
<ul>
<li><span style="line-height: 1.572;">如果使用g标志，则将返回与完整正则表达式匹配的所有结果（<code>Array</code>），但不会返回捕获组，或者未匹配 <code>null</code>。</span></li>
<li>如果未使用g标志，则仅返回第一个完整匹配及其相关的捕获组<span style="line-height: 1.572;">（<code>Array</code>）</span>。 在这种情况下，返回的项目将具有如下所述的其他属性<span style="line-height: 1.572;">，或者未匹配 <code>null</code></span>。</li>
</ul>
<h4 id="附加属性">附加属性</h4>
<p>如上所述，匹配的结果包含如下所述的附加特性。</p>
<ul>
<li><code>groups</code>: 一个捕获组数组 或 <a href="Reference/Global_Objects/undefined" title="undefined是全局对象的一个属性。也就是说，它是全局作用域的一个变量。undefined的最初值就是原始数据类型undefined。"><code>undefined</code></a>（如果没有定义命名捕获组）。</li>
<li><code>index</code>: 匹配的结果的开始位置</li>
<li><code>input</code>: 搜索的字符串.</li>
</ul>
<dl>
<dd>一个<a href="Reference/Array" title="REDIRECT Array"><code>Array</code></a>，其内容取决于global（<code>g</code>）标志的存在与否，如果未找到匹配则为<a href="Reference/Global_Objects/null" title="值 null 特指对象的值未设置。它是 JavaScript 基本类型 之一。"><code>null</code></a>。</dd>
</dl>
<h2 id="Description" name="Description">描述</h2>
<p>如果正则表达式不包含 <code>g </code>标志，<code>str.match()</code> 将返回与 <a href="Reference/Global_Objects/RegExp/exec" title="exec() 方法在一个指定字符串中执行一个搜索匹配。返回一个结果数组或 null。"><code>RegExp.exec()</code></a>. 相同的结果。</p>
<h3 id="Notes" name="Notes">参看：<code>RegExp</code> 方法</h3>
<ul>
<li>如果你需要知道一个字符串是否与一个正则表达式匹配 <a href="Reference/RegExp" title="此页面仍未被本地化, 期待您的翻译!"><code>RegExp</code></a> ，可使用 <a href="Reference/Global_Objects/RegExp/test" title="test() 方法执行一个检索，用来查看正则表达式与指定的字符串是否匹配。返回 true 或 false。"><code>test()</code></a> 。</li>
<li>如果你只是需要第一个匹配结果，你也可以使用 <a href="Reference/Global_Objects/RegExp/exec" title="exec() 方法在一个指定字符串中执行一个搜索匹配。返回一个结果数组或 null。"><code>RegExp.exec()</code></a> 。</li>
<li>如果你想要获得捕获组，并且设置了全局标志，你需要用 <a href="Reference/Global_Objects/RegExp/exec" title="exec() 方法在一个指定字符串中执行一个搜索匹配。返回一个结果数组或 null。"><code>RegExp.exec()</code></a> 。</li>
</ul>
<h2 id="Examples" name="Examples">示例</h2>
<h3 id="Example:_Using_match" name="Example:_Using_match">例子：使用 <code>match</code></h3>
<p>在下例中，使用 <code>match</code> 查找 "<code>Chapter</code>" 紧跟着 1 个或多个数值字符，再紧跟着一个小数点和数值字符 0 次或多次。正则表达式包含 <code>i</code> 标志，因此大小写会被忽略。</p>
<pre><code  class="language-javascript"><code class="language-js"><span class="keyword token">var</span> str <span class="operator token">=</span> <span class="string token">'For more information, see Chapter 3.4.5.1'</span><span class="punctuation token">;</span>
<span class="keyword token">var</span> re <span class="operator token">=</span> <span class="regex token">/see (chapter \d+(\.\d)*)/i</span><span class="punctuation token">;</span>
<span class="keyword token">var</span> found <span class="operator token">=</span> str<span class="punctuation token">.</span><span class="function token">match</span><span class="punctuation token">(</span>re<span class="punctuation token">)</span><span class="punctuation token">;</span>

console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>found<span class="punctuation token">)</span><span class="punctuation token">;</span>

<span class="comment token">// logs [ 'see Chapter 3.4.5.1',</span>
<span class="comment token">//        'Chapter 3.4.5.1',</span>
<span class="comment token">//        '.1',</span>
<span class="comment token">//        index: 22,</span>
<span class="comment token">//        input: 'For more information, see Chapter 3.4.5.1' ]</span>

<span class="comment token">// 'see Chapter 3.4.5.1' 是整个匹配。</span>
<span class="comment token">// 'Chapter 3.4.5.1' 被'(chapter \d+(\.\d)*)'捕获。</span>
<span class="comment token">// '.1' 是被'(\.\d)'捕获的最后一个值。</span>
<span class="comment token">// 'index' 属性(22) 是整个匹配从零开始的索引。</span>
<span class="comment token">// 'input' 属性是被解析的原始字符串。</span></code></code></pre>
<h3 id="Example:_Using_global_and_ignore_case_flags_with_match" name="Example:_Using_global_and_ignore_case_flags_with_match">例子：<code>match</code> 使用全局（global）和忽略大小写（ignore case）标志</h3>
<p>下例展示了 <code>match</code> 使用 global 和 ignore case 标志。A-E、a-e 的所有字母将会作为一个数组的元素返回。</p>
<pre><code  class="language-javascript"><code class="language-js"><span class="keyword token">var</span> str <span class="operator token">=</span> <span class="string token">'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'</span><span class="punctuation token">;</span>
<span class="keyword token">var</span> regexp <span class="operator token">=</span> <span class="regex token">/[A-E]/gi</span><span class="punctuation token">;</span>
<span class="keyword token">var</span> matches_array <span class="operator token">=</span> str<span class="punctuation token">.</span><span class="function token">match</span><span class="punctuation token">(</span>regexp<span class="punctuation token">)</span><span class="punctuation token">;</span>

console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>matches_array<span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="comment token">// ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']</span></code></code></pre>
<h3 id="使用match()，不传参数"><code>使用match()，不传参数 </code></h3>
<pre><code  class="language-javascript"><code class="language-js"><span class="keyword token">var</span> str <span class="operator token">=</span> <span class="string token">"Nothing will come of nothing."</span><span class="punctuation token">;</span>

str<span class="punctuation token">.</span><span class="function token">match</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">;</span>   <span class="comment token">// returns [""]</span></code></code></pre>
<h3 id="一个非正则表达式对象作为参数">一个非正则表达式对象作为参数</h3>
<p>当参数是一个字符串或一个数字，它会使用new RegExp(obj)来隐式转换成一个 <a href="Reference/RegExp" title="此页面仍未被本地化, 期待您的翻译!"><code>RegExp</code></a>。如果它是一个有正号的正数，RegExp() 方法将忽略正号。</p>
<pre><code  class="language-javascript"><code class="language-js"><span class="keyword token">var</span> str1 <span class="operator token">=</span> <span class="string token">"NaN means not a number. Infinity contains -Infinity and +Infinity in JavaScript."</span><span class="punctuation token">,</span>
    str2 <span class="operator token">=</span> <span class="string token">"My grandfather is 65 years old and My grandmother is 63 years old."</span><span class="punctuation token">,</span>
    str3 <span class="operator token">=</span> <span class="string token">"The contract was declared null and void."</span><span class="punctuation token">;</span>
str1<span class="punctuation token">.</span><span class="function token">match</span><span class="punctuation token">(</span><span class="string token">"number"</span><span class="punctuation token">)</span><span class="punctuation token">;</span>   <span class="comment token">// "number" 是字符串。返回["number"]</span>
str1<span class="punctuation token">.</span><span class="function token">match</span><span class="punctuation token">(</span><span class="number token">NaN</span><span class="punctuation token">)</span><span class="punctuation token">;</span>        <span class="comment token">// NaN的类型是number。返回["NaN"]</span>
str1<span class="punctuation token">.</span><span class="function token">match</span><span class="punctuation token">(</span><span class="number token">Infinity</span><span class="punctuation token">)</span><span class="punctuation token">;</span>   <span class="comment token">// Infinity的类型是number。返回["Infinity"]</span>
str1<span class="punctuation token">.</span><span class="function token">match</span><span class="punctuation token">(</span><span class="operator token">+</span><span class="number token">Infinity</span><span class="punctuation token">)</span><span class="punctuation token">;</span>  <span class="comment token">// 返回["Infinity"]</span>
str1<span class="punctuation token">.</span><span class="function token">match</span><span class="punctuation token">(</span><span class="operator token">-</span><span class="number token">Infinity</span><span class="punctuation token">)</span><span class="punctuation token">;</span>  <span class="comment token">// 返回["-Infinity"]</span>
str2<span class="punctuation token">.</span><span class="function token">match</span><span class="punctuation token">(</span><span class="number token">65</span><span class="punctuation token">)</span><span class="punctuation token">;</span>         <span class="comment token">// 返回["65"]</span>
str2<span class="punctuation token">.</span><span class="function token">match</span><span class="punctuation token">(</span><span class="operator token">+</span><span class="number token">65</span><span class="punctuation token">)</span><span class="punctuation token">;</span>        <span class="comment token">// 有正号的number。返回["65"]</span>
str3<span class="punctuation token">.</span><span class="function token">match</span><span class="punctuation token">(</span><span class="keyword token">null</span><span class="punctuation token">)</span><span class="punctuation token">;</span>       <span class="comment token">// 返回["null"]</span></code></code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf" hreflang="en" lang="en" rel="noopener" title="ECMAScript 3rd Edition (ECMA-262)">ECMAScript 3rd Edition (ECMA-262)</a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>Initial definition. Implemented in JavaScript 1.2.</td>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/5.1/#sec-15.5.4.10" hreflang="en" lang="en" rel="noopener">ECMAScript 5.1 (ECMA-262)<br/><small lang="zh-CN">String.prototype.match</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td> </td>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-string.prototype.match" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">String.prototype.match</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td> </td>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-string.prototype.match" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">String.prototype.match</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p></p><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
            This compatibility table still uses the old format,
            because we haven't yet converted the data it contains.
            <strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div><p></p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
</tr>
</tbody>
</table>
</div>
<h2 id="Firefox_特殊注意">Firefox 特殊注意</h2>
<ul>
<li>从Gecko 27 (Firefox 27 / Thunderbird 27 / SeaMonkey 2.24)开始，此方法遵守ECMAScript 规范。当使用全局正则表达式调用 <code>match()时，</code><a href="Reference/Global_Objects/RegExp/lastIndex" title="lastIndex 是正则表达式的一个可读可写的整型属性，用来指定下一次匹配的起始索引。"><code>RegExp.lastIndex</code></a> 属性(如果指定)会重置为 <code>0</code> (<a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=501739" rel="noopener" title="FIXED: String match and replace methods do not update global regexp lastIndex per ES3&amp;5">bug 501739</a>)。</li>
<li>从 Gecko 39 (Firefox 39 / Thunderbird 39 / SeaMonkey 2.36)开始，不赞成使用非标准的标志参数，并抛出一个控制台警告(<a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1142351" rel="noopener" title="FIXED: Add console warnings for non-standard flag argument of String.prototype.{search,match,replace}.">bug 1142351</a>)。</li>
<li>从 Gecko 47 (Firefox 47 / Thunderbird 47 / SeaMonkey 2.44) 开始 ， non-release builds不再支持非标准的标志参数，并且将完全移除(<a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1245801" rel="noopener" title="FIXED: Disable non-standard flag argument of String.prototype.{search,match,replace} in non-release build.">bug 1245801</a>)。</li>
<li>从 Gecko 49 (Firefox 49 / Thunderbird 49 / SeaMonkey 2.46) 开始 ， 不再支持非标准的标志参数 (<a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1108382" rel="noopener" title="FIXED: Remove non-standard flag argument from String.prototype.{search,match,replace}">bug 1108382</a>).</li>
</ul>
<h2 id="See_also" name="See_also">相关链接</h2>
<ul>
<li><a href="Reference/RegExp" title="此页面仍未被本地化, 期待您的翻译!"><code>RegExp</code></a></li>
<li><a href="Reference/Global_Objects/RegExp/exec" title="exec() 方法在一个指定字符串中执行一个搜索匹配。返回一个结果数组或 null。"><code>RegExp.prototype.exec()</code></a></li>
<li><a href="Reference/Global_Objects/RegExp/test" title="test() 方法执行一个检索，用来查看正则表达式与指定的字符串是否匹配。返回 true 或 false。"><code>RegExp.prototype.test()</code></a></li>
</ul>
</article>